//
//  CCMainTabBarController.swift
//  CCweibo
//
//  Created by itcc on 15/11/24.
//  Copyright © 2015年 itcc. All rights reserved.
//

import UIKit

class CCMainTabBarController: UITabBarController {

    /*
        事件写完之后会出现一个bug
        button整体往右边移动了一个按钮的位置 
    */
    ///撰写按钮点击事件
    func composeClick()
    {
        print("111")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        /* 设置撰写按钮的第一种方法 CCTabBar.swift
        
        //设置自定义tabBar 利用kvc设值 因为系统tabbar没有公开设值方法
        let newTabBar = CCTabBar()
        setValue(newTabBar, forKey: "tabBar")
        
        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        */
        
        //图片的颜色跟文字的颜色一样才用此方法
        tabBar.tintColor = UIColor.orangeColor()
        
        //首页
        let homeVC = CCHomeTableViewController()
        addChildViewController1(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let messageVC = CCMessageTableViewController()
        addChildViewController1(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //占位控制
        addChildViewController(UIViewController())
        
        //发现
        let discoverVC = CCDiscoverTableViewController()
        addChildViewController1(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVC = CCProfileTableViewController()
        addChildViewController1(profileVC, title: "我", imageName: "tabbar_profile")
        
        
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }
    
    
    /*
        自定义添加tabbar子控制器
        controller 子控制器
        title 子控制器的标题
        imageName 子控制器的图片名称
    
        private 表示只有本类或当前文件的其他类可以访问
    */
    private func addChildViewController1(controller: UIViewController, title: String, imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //拼接高亮图片名称
        let highlightedImageName = imageName + "_highlighted"
        //让选中的图片使用原来的颜色 不要系统帮我们渲染
//        let highlightedImage =
        UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        
        /*  设置高亮图片和标题字体颜色一样的第一种方法
        
        //设置高亮状态
//        controller.tabBarItem.selectedImage = highlightedImage
        
        //设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Highlighted)
        */
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }

    
    //懒加载 撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置参数
        //设置按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabbar里面
        self.tabBar.addSubview(button)
        
        //添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    

    
}
